﻿/*///////////////////////////////////////////////////////////
TodoHeaderBar.as
Ben Smith
2009
///////////////////////////////////////////////////////////*/

package com.bsmith.app.presentation 
{
	import com.bsmith.app.model.TodoAppState;
	import com.bsmith.app.model.TodoEvent;
	import com.bsmith.app.model.TodoItem;
	import com.bsmith.common.EventManager;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	/*===========================================================
	TodoHeaderBar
	Header bar for todo list
	===========================================================*/
	public class TodoHeaderBar extends Sprite
	{
		public var _smcBar:MovieClip;
		public var _stxTodoInput:TextField;
		public var _stxLeft:TextField;
		public var _stxCompleted:TextField;
		public var _sbtMinimize:SimpleButton;
		
		private var _bNextClickClearsTodo:Boolean = true;	//Should the next click clear the todo box?
		
		/*-----------------------------------------------------------
		TodoHeaderBar - ctor
		-----------------------------------------------------------*/
		public function TodoHeaderBar() 
		{
			_stxLeft.mouseEnabled = false;
			_stxCompleted.mouseEnabled = false;
			resetTodoInput();
		}	
		
		/*-----------------------------------------------------------
		initialize
		Initializes header bar and sets up listeners
		-----------------------------------------------------------*/
		public function initialize()
		{
			_smcBar.addEventListener(MouseEvent.MOUSE_DOWN, onBeginDragApp, false, 0, true);
			_stxTodoInput.addEventListener(MouseEvent.MOUSE_DOWN, onClickClearTodo, false, 0, true);
			_stxTodoInput.addEventListener(KeyboardEvent.KEY_DOWN, onTextInput, false, 0, true);
			_sbtMinimize.addEventListener(MouseEvent.CLICK, onClickMinimize, false, 0, true);
			
			TodoAppState.instance.addEventListener(TodoAppState.EVENT_CHANGED, onTodoNumChanged, false, 0, true);
		}
		
		/*-----------------------------------------------------------
		onClickMinimize
		When the user clicks the minimize button, minimize the
		app
		-----------------------------------------------------------*/
		private function onClickMinimize(e:MouseEvent):void 
		{
			this.dispatchEvent(new Event(Todo.EVENT_FORCE_MINIMIZE));
		}
		
		/*-----------------------------------------------------------
		onTodoNumChanged
		When the number of todo items change, update header text
		-----------------------------------------------------------*/
		private function onTodoNumChanged(e:Event=null):void
		{
			if (!TodoAppState.instance.getTodoList(TodoAppState.TODO_ACTIVE) || !TodoAppState.instance.getTodoList(TodoAppState.TODO_COMPLETED))
				return;
			var numActive:int = TodoAppState.instance.getTodoList(TodoAppState.TODO_ACTIVE).getNumItems();
			var numCompleted:int = TodoAppState.instance.getTodoList(TodoAppState.TODO_COMPLETED).getNumItems();
			_stxLeft.text = "to-do " + numActive;
			_stxCompleted.text = "complete " + numCompleted;
		}
		
		/*-----------------------------------------------------------
		onTextInput
		When the user presses a key, checks for the enter key
		-----------------------------------------------------------*/
		private function onTextInput(e:KeyboardEvent):void 
		{
			tryClearTodo();
			switch (e.keyCode)
			{
				case Keyboard.ENTER:
				{
					if (inputBlank())
					{
						//EventManager.instance.dispatchEvent(new TodoEvent(TodoEvent.BAD_TODO_ENTRY, { entry:_stxTodoInput.text } ));
					}
					else
						TodoAppState.instance.getTodoList(TodoAppState.TODO_ACTIVE).addItem(new TodoItem(_stxTodoInput.text));
					resetTodoInput();
				} break;
			}
		}
		
		/*-----------------------------------------------------------
		inputBlank
		Is the input window blank?
		-----------------------------------------------------------*/
		private function inputBlank():Boolean
		{
			for (var i:int = 0; i < _stxTodoInput.text.length; ++i)
			{
				var s:String = _stxTodoInput.text.charAt(i);
				if (s != " ")
					return false;
			}
			return true;
		}
		
		/*-----------------------------------------------------------
		resetTodoInput
		Resets the todo input box
		-----------------------------------------------------------*/
		private function resetTodoInput():void
		{
			_stxTodoInput.text = "Enter a todo here..."
			_stxTodoInput.setSelection(_stxTodoInput.length, _stxTodoInput.length);
			_bNextClickClearsTodo = true;
		}
		
		/*-----------------------------------------------------------
		onClickClearTodo
		When the user clicks the todo box, may have to clear it
		-----------------------------------------------------------*/
		private function onClickClearTodo(e:MouseEvent):void 
		{
			tryClearTodo();
		}
		
		/*-----------------------------------------------------------
		tryClearTodo
		Attempts to clear the todo box
		-----------------------------------------------------------*/
		private function tryClearTodo():void
		{
			if (!_bNextClickClearsTodo)
				return;
			_stxTodoInput.text = "";
			_bNextClickClearsTodo = false;
		}
		
		/*-----------------------------------------------------------
		onBeginDragApp
		Begins dragging the application
		-----------------------------------------------------------*/
		private function onBeginDragApp(e:MouseEvent):void 
		{
			this.stage.nativeWindow.startMove();
		}
	}
}